/**
 * @file 用户绑定角色管理
 * @date 2025/09/01 16:03:53
 * @author lyqjob@yeah.net
 */

import { useState,useEffect } from 'react';
import { Table, Tag, Space ,Button , message} from 'antd';
import AuthButton from '@/components/AuthButton';
import { adminApi } from "@/services/admin";
import dayjs from '@/utils/dayjs.js';



const Rolelist = (props) => {
    const {rowId} = props;
    const [data,setData] = useState([]);
    const [loading,setLoading] =useState(true)
    const [pageSize,setPageSize] = useState(10)
    const [current,setCurrent] = useState(1)
    const [total] = useState(0)
    

    const columns = [
        { title: '角色编码', dataIndex: 'code', key: '' },
        {
            title: '角色名称',
            dataIndex: 'name',
            key: '',
        },
        // { title: '权限字符', dataIndex: 'auth', key: '' },
        {
            title: '状态', dataIndex: 'status', key: '',
            render: (tag) => {
                return (
                    <Tag color={tag===0?'green':'red'} key={tag}>
                        {tag===0?'正常':'停用'}
                    </Tag>
                )
            }
        },
        { title: '创建时间', dataIndex: 'created_at', key: '',
            render:(text) => dayjs(text).format('YYYY-MM-DD HH:mm:ss')
        },
        { title: '绑定时间', dataIndex: 'bind_time', key: '',
            render:(text) => dayjs(text).format('YYYY-MM-DD HH:mm:ss')
        },
        
        {
            title: '操作',
            dataIndex: '',
            key: 'options',
            render: (item) => (
                <Space >

                    {
                        item.is_protected ===0?  
                        <AuthButton permission={['user:unbindroles','role:list']}>
                            <a onClick={()=>removeBind(item.link_id)}>解绑</a>
                        </AuthButton>
                        :null
                    }
                </Space>
            )
        },
    ];

    
    useEffect(()=>{
        getList(current,pageSize)
    },[])


    const getList = async (page,limit)=>{
        
        const [err,res] = await adminApi.getBindroles({page,limit,userid:rowId});
        if(err){
            setLoading(false)
        }
        if(res.code ===200 && res?.data){
            setLoading(false)
            setData(res.data)
            
            window.process.done();
        }

    }

    const removeBind= async (id)=>{
        const [err,res] = await adminApi.delBindRole(id);
        if(err){
            message.error('解绑失败')
        }
        if(res.code === 200){
            message.success('解绑成功')
            getList(current,pageSize)
        }
        
    }

    return (
        <>
            <Space>
                <AuthButton permission={['user:bindroles','role:list']}>
                    <Button type="primary" style={{ marginBottom: 16 }}>
                        绑定角色
                    </Button>
                </AuthButton>
                
            </Space>
            <Table
                loading={loading}
                rowKey={'id'}
                columns={columns}
                dataSource={data}
                pagination={{
                    pageSize:pageSize,
                    current:current,
                    showPage:true,
                    total:total,
                    showTotal: (total, range) => `${range[0]}-${range[1]} 共 ${total} 条`,
                    onChange:(page,pageSize)=>{
                        setPageSize(pageSize)
                        setCurrent(page)
                        getList(page,pageSize)
                    }    
                }}
            />

            
        </>
    )
}


export default Rolelist;
